<?php
/**
 *
 */
class coin_logModel extends Model
{
    public function addLog($user_id, $game_log_id, $diamonds, $account_diamonds, $memo)
    {
        $user_id     = intval($user_id);
        $game_log_id = intval($game_log_id);
        $diamonds    = intval($diamonds);
        if (!($user_id && $game_log_id && $diamonds)) {
            return false;
        }
        $data = array(
            'user_id'          => $user_id,
            'game_log_id'      => $game_log_id,
            'diamonds'         => $diamonds,
            'account_diamonds' => $account_diamonds,
            'memo'             => $memo,
            'create_time'      => NOW_TIME,
        );
        return $this->insert($data);
    }
    public function multiAddLog($game_log_id, $result, $times)
    {
        $create_time    = NOW_TIME;
        $table_coin_log = DB_PREFIX . 'coin_log';
        $table          = DB_PREFIX . 'user_game_log';
        $table_user     = DB_PREFIX . 'user';
        self::$sql      = "INSERT INTO $table_coin_log (
                `user_id`,
                `game_log_id`,
                `diamonds`,
                `account_diamonds`,
                `memo`,
                `create_time`
            )
            SELECT
                `user_id`,
                '$game_log_id' AS `game_log_id`,
                (SUM(l.`money`) * $times) AS `diamonds`,
                u.`coin` AS `account_diamonds`,
                '投注中奖' AS memo,
                '$create_time' AS `create_time`
            FROM
                $table AS l,
                $table_user AS u
            WHERE
                l.user_id = u.id
            AND l.type = 1
            AND `game_log_id` = $game_log_id
            AND `bet` = $result
            GROUP BY
                `user_id`";
        return Connect::exec(self::$sql);
    }
    public function returnCoin($game_log_id)
    {
        $create_time    = NOW_TIME;
        $table_coin_log = DB_PREFIX . 'coin_log';
        $table          = DB_PREFIX . 'user_game_log';
        $table_user     = DB_PREFIX . 'user';
        self::$sql      = "INSERT INTO $table_coin_log (
                `user_id`,
                `game_log_id`,
                `diamonds`,
                `account_diamonds`,
                `memo`,
                `create_time`
            )
            SELECT
                `user_id`,
                '$game_log_id' AS `game_log_id`,
                SUM(l.`money`) AS `diamonds`,
                (u.`coin`) AS `account_diamonds`,
                '游戏终止，投注返还' AS memo,
                '$create_time' AS `create_time`
            FROM
                $table AS l,
                $table_user AS u
            WHERE
                l.user_id = u.id
            AND `game_log_id` = $game_log_id
            GROUP BY
                `user_id`";
        return Connect::exec(self::$sql);
    }
}
